<?php
class TownsController extends AppController {

    public $helpers = array('Html', 'Form');
    public $components = array('Session');

    public function index() {
        $pageTitle ="Worldwide Seventh-day Adventist Church - Kwabrafoso: Towns";
        $in_record_data = $this->Town->find('all');
        $this->set(compact('in_record_data','pageTitle'));
    }
    
    public function add(){
    $pageTitle ="Worldwide Seventh-day Adventist Church - Kwabrafoso: Towns";
     if($_POST){
       // pr($_POST);
        $data['Town'] = array(
            'divname' => $_POST['divname'],
            'uniname' => $_POST['uniname'],
            'conname' => $_POST['conname'],
            'regname' => $_POST['regname'],
            'distname' => $_POST['distname'],
            'townname' => $_POST['townname']
        );

        if(!$this->Town->save($data)){
            $this->Session->setFlash('Error Saving Town');
        }else{
             $this->Session->setFlash('Town Saved');
            $this->redirect('index');
        }
    }

    $this->set(compact('pageTitle','errorMsg'));
}
    
    // A functin to delete Records
    public function delete($id = null)
    {
       if (!$id) 
       {
        $this->Session->setFlash('Invalid id for Town');
        $this->redirect(array('action' => 'index'));
    }
    if ($this->Town->delete($id)) 
    {
        $this->Session->setFlash('Town Deleted Please');
    } else 
    {
        $this->Session->setFlash(__('Record Not Deleted', true));
    }
    $this->redirect(array('action' => 'index'));
}

    // A functin to edit Records
    function edit($id = null){
    $record_id = $id;
    $conditions = array('Town.id' => $record_id);
    $record = $this->Town->find('first', array('conditions' => $conditions, 'recursive' => 1));
   // pr($record);
            
     $pageTitle ="Worldwide SDA Church - Kwabrafoso: Edit A Town";
     if($_POST){
        //pr($_POST);
        $data['Town'] = array(
            'divname' => $_POST['divname'],
            'uniname' => $_POST['uniname'],
            'conname' => $_POST['conname'],
            'regname' => $_POST['regname'],
            'distname' => $_POST['distname'],
            'townname' => $_POST['townname']
        );

        if(!$this->Town->save($data)){
            $this->Session->setFlash('Error Saving Town');
        }else{
             $this->Session->setFlash('Town Saved');
            $this->redirect('index');
        }
    }

    $this->set(compact('pageTitle','record'));
}

     // A functin to view Records
    public function view($id) {
    if (!$id) {
        throw new NotFoundException(__('Invalid Record'));
    }

    $district = $this->Town->findById($id);
    if (!$district) {
        throw new NotFoundException(__('Invalid Record'));
    }
    $this->set('district', $district);
}

function report($id = null){
    $record_id = $id;
    $conditions = array('Town.id' => $record_id);
    $record = $this->Town->find('first', array('conditions' => $conditions, 'recursive' => 1));
//    pr($record);

}

}
?>
